package com.openingedu.mba.screen.adapter;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lurencun.android.support.widget.CommonAdapter;
import com.openingedu.mba.R;
import com.openingedu.mba.common.AppSetting;
import com.openingedu.mba.common.CommonUtil;
import com.openingedu.mba.persist.DbHistory;

public class HistoryAdapter extends CommonAdapter<DbHistory>{

	private Context mContext;
	
	public HistoryAdapter(Context context) {
		super(context);
		mContext = context;
	}

	@Override
	protected View createView(LayoutInflater inflater, final DbHistory data,
			int position, View convertView, ViewGroup parent) {
		LinearLayout messageLayout = (LinearLayout) inflater.inflate(R.layout.item_history, null);
		TextView totalUsedTimeView = (TextView) messageLayout.findViewById(R.id.total_used_time_view);
		TextView averageUsedTimeView = (TextView)  messageLayout.findViewById(R.id.average_used_time_view);
		TextView correctNumView = (TextView)  messageLayout.findViewById(R.id.correct_num_view);
		TextView faultNumView = (TextView)  messageLayout.findViewById(R.id.fault_num_view);
		TextView scoreNumView = (TextView)  messageLayout.findViewById(R.id.score_view);
		TextView valuationView = (TextView)  messageLayout.findViewById(R.id.val_content_view);
		TextView dateView = (TextView)  messageLayout.findViewById(R.id.history_date_view);
		totalUsedTimeView.setText(data.getTimeUsed());
		averageUsedTimeView.setText(data.getAverageTimeUsed());
		correctNumView.setText("" + data.getCorrectNum() + "题");
		faultNumView.setText("" + data.getFaultNum() + "题");
		scoreNumView.setText("" + data.getTotalScore() + "分，正确率:" + CommonUtil.percentStr(data.getCorrectNum(), data.getTotalNum()));
		int val = data.getValuation();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateView.setText(dateFormat.format(data.getPracticeDate()));
		if (val == AppSetting.Valuation.PERFECT) {
			valuationView.setText("Near perfection!");
			valuationView.setTextColor(mContext.getResources().getColor(R.color.greenyellow));
		}
		else if (val == AppSetting.Valuation.GOOD) {
			valuationView.setText("Good work!");
			valuationView.setTextColor(mContext.getResources().getColor(R.color.greenyellow));
		}
		else if (val == AppSetting.Valuation.PASSING) {
			valuationView.setText("Success.");
			valuationView.setTextColor(mContext.getResources().getColor(R.color.orange));
		}
		else if (val == AppSetting.Valuation.BAD) {
			valuationView.setText("Work hard.");
			valuationView.setTextColor(mContext.getResources().getColor(R.color.red));
		}
		else if (val == AppSetting.Valuation.POOR) {
			valuationView.setText("Managing Time!");
			valuationView.setTextColor(mContext.getResources().getColor(R.color.lightgray));
		}
		return messageLayout;
	}
}
